﻿CREATE PROCEDURE [util].[GetSQLDataRoot]
@SQLDataRoot NVARCHAR (256) OUTPUT
AS
BEGIN	
	CREATE TABLE #XP_OUTPUT(
		[Data] nvarchar(256) COLLATE DATABASE_DEFAULT
		,[Value] nvarchar(256) COLLATE DATABASE_DEFAULT
	)

	INSERT #XP_OUTPUT ([Data],[Value])
	EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE'
		,N'SOFTWARE\Microsoft\MSSQLServer\Setup'
		,N'SQLDataRoot'

	SELECT
		@SQLDataRoot = [Value]
	FROM #XP_OUTPUT
	WHERE([Data] = N'SQLDataRoot')

	DROP TABLE #XP_OUTPUT

	IF(@SQLDataRoot IS NULL)
		BEGIN
			RAISERROR('Unable to retrieve SQLDataRoot registry string value',16,1)
			RETURN(@@ERROR)
		END
	ELSE
		RETURN(0)
END